<template>
  <div class="sub-category" v-if="subCate">
    <div class="container">
      <!-- 面包屑 -->
      <SubBread />
      <!-- 筛选分区 -->
      <Sub-filter />
      <!-- 结果区域 -->
      <div class="goods-list">
        <!-- 排序 -->
        <SubSort />
        <!-- 商品列表 -->

         <XtxInfiniteLoading  :finished="finished"
          :v-model="loading"  @load="getList"  title="没有东西了" >
           <ul class="list">
          <li v-for="item in list" :key="item.id">
            <GoodsItem :goods="item"></GoodsItem>
          </li>
        </ul>
          </XtxInfiniteLoading>
      </div>
    </div>
  </div>
  <!-- 骨架屏 -->
  <div v-else class="sub-category">
    <XtxSkeleton class="item" width="800px" height="40px"  />
    <XtxSkeleton class="item" width="800px" height="40px"  />
    <XtxSkeleton class="item" width="600px" height="40px"  />
    <XtxSkeleton class="item" width="600px" height="40px"  />
    <XtxSkeleton class="item" width="600px" height="40px"  />
  </div>
</template>

<script>
import SubBread from './components/sub-bread.vue'
import SubFilter from './components/sub-filter.vue'
import SubSort from './components/sub-sort.vue'
import GoodsItem from './components/goods-item.vue'
import { findCategoryGoods, findSubCategory } from '@/api/category'
import { useRoute } from 'vue-router'
import { provide, ref, watch, reactive } from 'vue'
export default {
  name: 'SubCategory',
  components: { SubBread, SubFilter, SubSort, GoodsItem },
  setup () {
    const route = useRoute()
    const subCate = ref(null)
    // 根据路由的分类 id 获取二级分类数据
    const loading = ref(false)
    const finished = ref(false)
    const defaultParams = () => {
      return {
      //  取值范围 [publishTime,orderNum,evaluateNum,price]
        brandId: null,
        inventory: false,
        onlyDiscount: false,
        sortField: null,
        sortMethod: null
      }
    }
    const brands = reactive({})
    watch(
      () => route.params.id,
      async id => {
        // 如果当前不是二级分类页，直接退出，不发送二级分类请求
        if (route.path !== `/category/sub/${id}`) return
        // 重新发送请求，已经重置品牌字段
        const { result } = await findSubCategory(id)
        subCate.value = result
        brands.value = { id: null, name: '品牌', properties: subCate.value.brands, activeName: null }
        const obj = defaultParams()
        for (const key in obj) {
          sortParams[key] = obj[key]
        }
      },
      { immediate: true }
    )
    // 通过 provide 提供数据给所有的后代组件
    provide('subCate', subCate)
    provide('brands', brands)
    const list = ref([])
    const sortParams = reactive({
      categoryId: route.params.id,
      //  取值范围 [publishTime,orderNum,evaluateNum,price]
      inventory: false,
      onlyDiscount: false,
      sortField: null,
      sortMethod: null,
      attrs: [],
      brandId: null,
      page: 1,
      pageSize: 20
    })
    provide('sortParams', sortParams)
    // 根据请求参数获取商品列表数据
    const getList = async () => {
      loading.value = true
      // 📦商品列表--分页查询      - 数据量少
      const { result } = await findCategoryGoods(sortParams)
      // 📦商品列表--分页查询-临时 - 数据量大(但是数据是假的，主要用于实现分页)
      // const { result } = await findCategoryGoodsTemp(sortParams)
      loading.value = false
      list.value.push(...result.items)
      sortParams.page++
      if (result.items.length < sortParams.pageSize) {
        finished.value = true
      }
    }
    // 当发生筛选时，应该把页码重置为1，然后把展示数组重置为请求到的新数据，最后再把页码改为2，这样分页的话就会从第二页开始请求
    const setList = async () => {
      sortParams.page = 1
      const { result } = await findCategoryGoods(sortParams)
      list.value = result.items
      sortParams.page++
    }
    provide('setList', setList)
    // watch(sortParams, () => {
    //   getList()
    // }, { immediate: true })

    return { subCate, getList, list, brands, loading, finished }
  }
}
</script>

<style scoped lang="less">
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li {
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
.xtx-skeleton {
    padding: 10px 0;
}
</style>
